package com.ruoqing.admin.test;

/**
	* @author YaoXian
	* @title: Solution
	* @projectName ruoxi-blog
	* @description: 若兮微服务 -
	* @date 2021-04-05 20:44
	*/
class Solution {
		public static void main(String[] args) {
				//[[1,3,5,7],[10,11,16,20],[23,30,34,60]]
				int [][] arr = {{1,3,5,7},{10,11,16,20},{23,30,34,60}};
				System.out.println(searchMatrix(arr, 13));
		}
		public static boolean searchMatrix(int[][] matrix, int target) {
				int n = matrix[0].length;
				for(int i = 0;i<matrix.length;i++){
						if(matrix[i][n-1] >= target){
								return binary(target,0,n-1,matrix,i);
						}
				}
				return false;
		}
		//二分查找
		public static boolean binary(int target,int left,int right,int [][] arr,int row){
				int middle = (left+right)/2;
				if(left > right){
						return false;
				}else if (	left == right && target == arr[row][middle]){
						return true;
				}else if (left == right){
						return false;
				}
				if(arr[row][middle] < target){
						//向右递归
						return binary(target,middle+1,right,arr,row);
				}else if(arr[row][middle] > target){
						//向左递归
						return binary(target,left,middle,arr,row);
				}else{
						return true;
				}
		}
}